﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UpdateControls.Correspondence;
using UpdateControls.Correspondence.Memory;

namespace SurveySez.Model.UnitTest
{
	[TestClass]
	public class EmceeEditsGames
	{
		private Community _community;
		private Emcee _emcee;

		[TestInitialize]
		public void Init()
		{
			_community = new Community(new MemoryStorageStrategy())
				.RegisterAssembly(typeof(Emcee));
			_emcee = _community.AddFact(new Emcee());
		}

		[TestMethod]
		public void GameListIsInitiallyEmpty()
		{
			Assert.IsFalse(_emcee.EmceeGames.Any());
		}

		[TestMethod]
		public void EmceeCreatesAGame()
		{
			Game game = _emcee.NewGame().Game;
			Assert.AreSame(game, _emcee.EmceeGames.Single().Game);
		}

		[TestMethod]
		public void GameNameIsInitiallyNull()
		{
			Game game = _emcee.NewGame().Game;
			Assert.IsNull(game.Name);
		}

		[TestMethod]
		public void EmceeNamesAGame()
		{
			Game game = _emcee.NewGame().Game;
			game.Name = "Retreat 9/8/2010";
			Assert.AreEqual("Retreat 9/8/2010", game.Name);
		}

		[TestMethod]
		public void EmceeRenamesAGame()
		{
			Game game = _emcee.NewGame().Game;
			game.Name = "Retreat 9/8/2010";
			game.Name = "Postponed retreat 9/15/2010";
			Assert.AreEqual("Postponed retreat 9/15/2010", game.Name);
		}

		[TestMethod]
		public void GameInitiallyHasNoSurveys()
		{
			Game game = _emcee.NewGame().Game;
			Assert.IsFalse(game.GameSurveys.Any());
		}

		[TestMethod]
		public void EmceeAddsASurvey()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			Assert.AreSame(survey, game.GameSurveys.Single().Survey);
		}

		[TestMethod]
		public void SurveyIsInAGame()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			Assert.AreSame(game, survey.Games.Single());
		}

		[TestMethod]
		public void EmceeRemovesASurvey()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			game.GameSurveys.Single().Delete();
			Assert.IsFalse(game.GameSurveys.Any());
		}

		[TestMethod]
		public void SurveyIsNotInAGame()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			game.GameSurveys.Single().Delete();
			Assert.IsFalse(survey.Games.Any());
		}

		[TestMethod]
		public void EmceeReAddsAGame()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			game.GameSurveys.Single().Delete();
			game.AddSurvey(survey);
			Assert.AreSame(survey, game.GameSurveys.Single().Survey);
		}

		[TestMethod]
		public void SurveyIsInAGameAgain()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey);
			game.GameSurveys.Single().Delete();
			game.AddSurvey(survey);
			Assert.AreSame(game, survey.Games.Single());
		}

		[TestMethod]
		public void EmceeAddsTwoSurveys()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey1 = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey1);
			Survey survey2 = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey2);
			Survey[] surveys = game.GameSurveys
				.OrderBy(gs => gs.Order)
				.Select(gs => gs.Survey)
				.ToArray();
			Assert.AreSame(survey1, surveys[0]);
			Assert.AreSame(survey2, surveys[1]);
		}

		[TestMethod]
		public void EmceeInsertsASurvey()
		{
			Game game = _emcee.NewGame().Game;
			Survey survey1 = _emcee.NewSurvey().Survey;
			game.AddSurvey(survey1);
			Survey survey2 = _emcee.NewSurvey().Survey;
			game.InsertSurvey(survey2, game.GameSurveys.Single(gs => gs.Survey == survey1));
			Survey[] surveys = game.GameSurveys
				.OrderBy(gs => gs.Order)
				.Select(gs => gs.Survey)
				.ToArray();
			Assert.AreSame(survey2, surveys[0]);
			Assert.AreSame(survey1, surveys[1]);
		}
	}
}
